#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2022/12/13 17:00
# @Author  : cry
# @File    : robot.py
# @Description : 交互，调用回复接口

import requests
import json

## 高德key
gdKey = 'xxx'


## 调用青云客的API，免费的API

def qingyunke(msg: str):
    """
    调用青云客api进行传输聊天内容获取机器人回复
    :param msg: 聊天内容
    :return: api回复内容
    """
    data = requests.get("http://api.qingyunke.com/api.php?key=free&appid=0&msg=" + msg).content
    data = json.loads(data)
    data = data['content'].replace("{br}", "\n")
    return data


## 调用高德天气的API，免费的API
def gaodeWeather(msg: str):
    """
    调用高德天气的api进行获取天气信息
    :param msg: 城市名称
    :return: 天气信息
    """
    data = requests.get("https://restapi.amap.com/v3/weather/weatherInfo?city=" + msg + "&key=" + gdKey).content
    data = json.loads(data)
    data = data['lives'][0]
    if data == []:
        return "请按照格式输入：城市名+的天气，如：曹县的天气"
    else:
        return "为您提供：" + data['province'] + "省" + data['city'] + "的天气：" + data['weather'] + ",温度在" + data[
            'temperature'] + "°左右\n" \
               + data['winddirection'] + "风，风级" + data['windpower'] + "\n" + "空气湿度为：" + data[
                   'humidity'] + "\n" + "发布时间：" + data["reporttime"]


## 在本地创建的医疗机器人
## 有兴趣的可以参考 https://zhuanlan.zhihu.com/p/379202949 文章内容，将 local.py 改成api形式即可。
def medical_robot(msg: str):
    url = "http://localhost:60063/service/api/medical_robot"
    data = {"question": msg}

    print('data', data)
    headers = {'Content-Type': 'application/json;charset=utf8'}
    response = requests.post(url, data=json.dumps(data), headers=headers)
    # print('response', response)
    if response.status_code == 200:
        response = json.loads(response.text)
        # print(response, '========')
        return response["data"]
    else:
        return "您的问题我无法理解，我还需要学习"
